// Vue
import Vue from 'vue'
import i18n from './i18n'
import App from './App'
// 核心插件
import d2Admin from '@/plugin/d2admin'
// store
import store from '@/store/index'
import axios from 'axios'
import VueAxios from 'vue-axios'

// 菜单和路由设置
import router from './router'
import { menuHeader, menuAside } from '@/menu'
import { frameInRoutes } from '@/router/routes'
import util from '@/libs/util.js'
// 组件
import TDesign from 'tdesign-vue'
// 引入组件库全局样式资源
import 'tdesign-vue/es/style/index.css'

// 引入指令
import intersect from '@/directive/intersect'
// 引入echarts
import * as echarts from 'echarts'
import dataV from '@jiaminghi/data-view'

Vue.prototype.$echarts = echarts
    // 核心插件
Vue.use(d2Admin)
Vue.use(dataV)
Vue.use(TDesign)
Vue.use(VueAxios, axios)
Vue.directive('intersect', intersect)
new Vue({
    router,
    store,
    i18n,
    render: h => h(App),
    created() {
        // 处理路由 得到每一级的路由设置
        this.$store.commit('d2admin/page/init', frameInRoutes)
            // 设置顶栏菜单
        this.$store.commit('d2admin/menu/headerSet', menuHeader)
            // 设置侧边栏菜单
        this.$store.commit('d2admin/menu/asideSet', menuAside)
            // 初始化菜单搜索功能
        this.$store.commit('d2admin/search/init', menuHeader)
    },
    mounted() {
        // 展示系统信息
        this.$store.commit('d2admin/releases/versionShow')
            // 用户登录后从数据库加载一系列的设置
        this.$store.dispatch('d2admin/account/load')
            // 获取并记录用户 UA
        this.$store.commit('d2admin/ua/get')
            // 初始化全屏监听
        this.$store.dispatch('d2admin/fullscreen/listen')
    }
}).$mount('#app')

axios.interceptors.request.use(config => {
    // 为请求头添加Authorization字段为服务端返回的token
    config.headers.Authorization = `Bearer${util.cookies.get('token')}`
        // return config是固定用法 必须有返回值
    return config
})